package com.estronger.shopmanage.adapter.tasking;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.DefaultAdapter;
import com.estronger.shopmanage.base.BaseHolder;
import com.estronger.shopmanage.bean.B101PgMeasureList.Datas.PgMeasureListData;
import com.estronger.shopmanage.bean.B11Designer;
import com.estronger.shopmanage.fragment.FragmentFactory;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.manager.PopManager;
import com.estronger.shopmanage.ui.measure.pg.MeasurePGActivity;
import com.estronger.shopmanage.ui.measure.pg.MsAffirmCondActivity;
import com.estronger.shopmanage.ui.measure.pg.MsAffirmPgActivity;
import com.estronger.shopmanage.ui.measure.pg.MsPgDetailActivity;
import com.estronger.shopmanage.utils.ConvertUtils;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.view.AppDialog;
import com.google.gson.Gson;
import com.lidroid.xutils.http.RequestParams;
import com.soonfor.evaluate.tools.CommonUtils;

import java.util.List;

public class TmsAdapter extends DefaultAdapter<PgMeasureListData> {

    private MeasurePGActivity mContext;
    private Gson gson;
    protected static final short READY_RECEIVE = 0;
    protected static final short READY_CONTACT = 1;
    protected static final short READY_ARRIVE = 2;
    private Dialog dialog;
    private View needDeleteView = null;
    private String needDeleteId = "";
    private MsHoder msHoder;
    private String type = "0";

    public TmsAdapter(MeasurePGActivity mActivity, List<PgMeasureListData> datas, Gson gson, String type) {
        super(datas);
        this.mContext = mActivity;
        this.gson = gson;
        this.type = type;
        // TODO Auto-generated constructor stub
    }

    @Override
    protected BaseHolder getHolder() {
        return new MsHoder();
    }

    // 当删除一个条目的动画执行完之后调用
    protected void deleteItem(String measureid) {
        if (TextUtils.isEmpty(measureid)) {
            return;
        }
        for (int i = 0; i < datas.size(); i++) {
            if ((datas.get(i).measureid).equals(measureid)) {
                datas.remove(datas.get(i));
                notifyDataSetChanged();
            }
        }

    }


    class MsHoder extends BaseHolder<PgMeasureListData> implements
            OnClickListener {
        String TitelName = "";
        public TextView tvfItemName, tvfItemLocation, tvfOrderTimeT, tvfOrderTime,
                tvfDesigner, tvfPgTime,tvftem_phoneno ;
        //public ImageView ivfCall, ivfSendMsg;
        public LinearLayout llfMeasureItem, llfHalfBottom, llfBottom_One, llfBottom_Two, llfYpgM;
        public RelativeLayout rlfLeftSure, rlfRightOrder;
        private PopManager popManager;
        private RequestParams params;
        private String _phone = "";//电话
        private int whitchToCall = 0;//是点击了那个按钮后跳出打电话对话框的
        private PgMeasureListData comData;

        /**
         * 初始化条目上的控件
         */
        @Override
        public View initView() {
            View view = null;
            view = View.inflate(mContext, R.layout.layout_pg_listitem, null);

            llfMeasureItem = (LinearLayout) view.findViewById(R.id.llMeasureItem);
            tvfItemName = (TextView) view.findViewById(R.id.tvfItemTitle);
            tvfItemLocation = (TextView) view.findViewById(R.id.tvfItemLocation);
            tvfOrderTimeT = (TextView) view.findViewById(R.id.tvfOrderTimeT);
            tvfOrderTime = (TextView) view.findViewById(R.id.tvfOrderTime);
//            ivfCall = (ImageView) view.findViewById(R.id.ivfMCall);
//            ivfSendMsg = (ImageView) view.findViewById(R.id.ivfMSendms);
            llfYpgM = (LinearLayout) view.findViewById(R.id.llfYpgM);
            llfHalfBottom = (LinearLayout) view.findViewById(R.id.llfHalfBottom);
            llfBottom_One = (LinearLayout) view.findViewById(R.id.llfBottom_One);
            llfBottom_Two = (LinearLayout) view.findViewById(R.id.llfBottom_Two);
            rlfLeftSure = (RelativeLayout) view.findViewById(R.id.rlfLeftSure);
            rlfRightOrder = (RelativeLayout) view.findViewById(R.id.rlfRightOrder);
            tvfDesigner = (TextView) view.findViewById(R.id.tvfDesigner);
            tvfPgTime = (TextView) view.findViewById(R.id.tvfPGTime);
            tvftem_phoneno = (TextView) view.findViewById(R.id.tvftem_phoneno);
            return view;
        }

        /**
         * 点击事件
         */
        @Override
        public void onClick(View v) {
            String measureid = "";
            if (v.getTag() != null) {
                measureid = String.valueOf(v.getTag());
            }
            Intent intent;
            switch (v.getId()) {
                case R.id.ivfMCall:// 点击了测量条目上的电话小图标
                    if (mContext.isRightNumber(_phone)) {
                        dialog = AppDialog.createPgDialog(mContext,
                                "[" + TitelName + "]", _phone, this, measureid);
                        whitchToCall = 0;
                        dialog.show();
                    }
                    break;
                case R.id.rlfLeftSure:
                    if (mContext.isRightNumber(_phone)) {
                        dialog = AppDialog.createPgDialog(mContext,
                                "[" + TitelName + "]", _phone, this, measureid);
                        whitchToCall = 1;
                        dialog.show();
                    }
                    break;
                case R.id.rlfRightOrder:// 点击了测量条目上的预约派工
                    if (mContext.isRightNumber(_phone)) {
                        dialog = AppDialog.createPgDialog(mContext,
                                "[" + TitelName + "]", _phone, this, measureid);
                        whitchToCall = 2;
                        dialog.show();
                    }
                    break;
                case R.id.ivfMSendms:// 点击了测量条目上的发短信小图标
                    if (mContext.isRightNumber(_phone)) {
                        Uri uri = Uri.parse("smsto:" + _phone);
                        intent = new Intent(Intent.ACTION_SENDTO, uri);
                        mContext.startActivity(intent);
                    }
                    break;
                case R.id.llMeasureItem:// 一个测量任务条目被点击了跳转到测量详情
                    intent = new Intent(mContext, MsPgDetailActivity.class);
                    intent.putExtra("measureItem", comData);
                    mContext.startActivity(intent);
                    break;
                case R.id.btnfSure:// 点击了联系客户的对话框的确定按钮的处理
                    if (PermissionUtils.isCallPhoneGranted(mContext)) {
                        intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                                + _phone));
                        if (whitchToCall == 1) {// 这里是判断拨打电话后是否需要跳转其它界面
                            MeasurePGActivity.CALL_RESULT_MSCONDITION = CommonUtils.getRequestCode(measureid);
                            MeasurePGActivity.pmData = comData;
                            mContext.startActivityForResult(intent,
                                    MeasurePGActivity.CALL_RESULT_MSCONDITION);
                        } else if (whitchToCall == 2) {
                            MeasurePGActivity.CALL_RESULT_PGSURE = CommonUtils.getRequestCode(measureid);
                            MeasurePGActivity.pmData = comData;
                            mContext.startActivityForResult(intent,
                                    MeasurePGActivity.CALL_RESULT_PGSURE);
                        } else {
                            mContext.startActivity(intent);
                        }
                    } else {
                        PermissionUtils.ToSetPermissions(mContext, R.string.PermissionsCallPhone);
                    }
                    dialog.dismiss();
                    break;
                case R.id.btnfCancel:
                    if (whitchToCall == 1) {
                        Intent intent1 = new Intent(mContext, MsAffirmCondActivity.class);
                        intent1.putExtra("measureItem", comData);
                        mContext.startActivityForResult(intent1, MeasurePGActivity.REFLESH_DATA);
                    } else if (whitchToCall == 2) {
                        Intent intent2 = new Intent(mContext, MsAffirmPgActivity.class);
                        intent2.putExtra("measureItem", comData);
                        mContext.startActivityForResult(intent2, MeasurePGActivity.REFLESH_DATA);
                    }
                    dialog.dismiss();
                    break;
            }
        }

        /*
         * 更新Item显示
         */
        @Override
        public void refreshView(PgMeasureListData data) {
            comData = data;
            _phone = data.cstmobile;
            if (type.equals("0")) {
//                ivfCall.setVisibility(View.GONE);
//                ivfSendMsg.setVisibility(View.GONE);
                tvfOrderTimeT.setText("下单时间:");
                llfYpgM.setVisibility(View.GONE);
                llfHalfBottom.setVisibility(View.VISIBLE);
                llfMeasureItem.setOnClickListener(this);
                tvfOrderTime.setText(data.ordrdate);
                tvftem_phoneno.setVisibility(View.VISIBLE);
                tvftem_phoneno.setText(_phone);
            } else if (type.equals("1")) {
                tvftem_phoneno.setVisibility(View.GONE);
//                ivfCall.setVisibility(View.VISIBLE);
//                ivfSendMsg.setVisibility(View.VISIBLE);
                tvfOrderTimeT.setText("预约时间:");
                llfYpgM.setVisibility(View.VISIBLE);
                llfHalfBottom.setVisibility(View.GONE);
                tvfDesigner.setText(data.plandesignername);
                tvfPgTime.setText(data.assigndate);
                tvfOrderTime.setText(data.planmeasuredate);
            }
//            ivfSendMsg.setOnClickListener(this);
//            ivfCall.setOnClickListener(this);
            TitelName = ConvertUtils.formatNullF(data.cstbuildname) + data.cstname;
            tvfItemName.setText(TitelName);
            tvfItemLocation.setText(data.address);
            llfBottom_Two.setTag(data.measureid);
                /*
             * 按钮的点击事件都需要measureid
			 */

            llfMeasureItem.setTag(data.measureid);
            rlfLeftSure.setTag(data.measureid);
            rlfRightOrder.setTag(data.measureid);
            llfBottom_One.setTag(data.measureid);

            rlfLeftSure.setOnClickListener(this);
            rlfRightOrder.setOnClickListener(this);
            llfBottom_One.setOnClickListener(this);
        }

        /**
         * 当前activity所有联网成功的回调
         *
         * @params code 各个接口返回的唯一识别码，用来判断是哪个接口成功返回
         * @params result 网络请求成功返回的数据
         */
        @Override
        protected void onNetSuccess(int code, String result) {
            if (NetManager.DESIGNERS == code) {
                B11Designer designer = gson.fromJson(result, B11Designer.class);
                if (designer.success) {
                    popManager.refleshTransPop(designer);
                }
            }
        }

        @Override
        protected void onNetErorr(int code, String result) {
            mContext.setProgresVisible(false);
            needDeleteView = null;
        }
    }

    /**
     * @param startHeight 控件的高度
     * @param v           执行动画的控件
     */
    private void removeAnim(int startHeight, final View v) {
        if (v == null)
            return;
        ValueAnimator animator = ValueAnimator.ofInt(startHeight, 0);
        animator.addUpdateListener(new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (Integer) animation.getAnimatedValue();
                LayoutParams params = v.getLayoutParams();
                params.height = value;
                v.setLayoutParams(params);
            }
        });
        animator.addListener(new AnimatorListener() {

            @Override
            public void onAnimationStart(Animator arg0) {
            }

            @Override
            public void onAnimationRepeat(Animator arg0) {
            }

            @Override
            public void onAnimationEnd(Animator arg0) {
                deleteItem(needDeleteId);
                FragmentFactory.createFragment(mContext.getCurrPosition())
                        .connect(mContext.getCurrPosition() + "");
            }

            @Override
            public void onAnimationCancel(Animator arg0) {

            }
        });
        animator.setDuration(500);
        animator.start();
    }
}
